package com.example.demo3.controller;

import com.example.demo3.entity.User;
import com.example.demo3.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class login {
    @Resource
    private UserService userService;


    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    public String login(Model model, String name, String pwd, HttpSession session){
        User user = userService.findByNameAndPassword(name,pwd);
        if (user!=null){
            session.setAttribute("user",user);
                return null;
        }else {
            model.addAttribute("message","账号或密码错误！");
            return "/templates/login.html";
        }
    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        return "/login.jsp";
    }
}
